<?php
namespace Index\Controller;

use Think\Controller;
use Think\Log;

class DutyController extends Controller
{

    function _initialize()
    {
        session("username", "admin_jspider");
    }


    public function _empty($viewname)
    {
        $user = array();
        $user['username'] = session("username");
        $this->assign($user);
        $this->display(":$viewname");
    }

    public function sec_unit()
    {
        if (IS_POST && IS_AJAX) {
            $workSection = M("Worksection");
            $workSection->create();
            Log::write(dump($workSection->data()));
            $workSection->add();
            //$WorkSection->add();
        } else {
            $this->error("非法请求");
        }
    }

    public function sec_udpate()
    {
        if (IS_POST && IS_AJAX) {
            $workSection = M("Worksection");
            $workSection->create();
            $workSection->save();
        } else {
            $this->error("非法请求");
        }

    }

    public function sec_list()
    {
        $sec = M("Worksection");
        $result = $sec->where(array("status" => 1))->field("sec_id,sec_name,sec_note")->order(array("sec_id" => "desc"))->select();
        Log::write(dump($result, false));
        $this->assign("sections", $result);
        $this->display(":work-section-role");
    }

    public function work_seciton_edit()
    {
        $id = intval(I("get.id"));
        $sec = M("Worksection");
        $result = $sec->where(array("sec_id" => $id))->find();
        Log::write(dump($result, false));
        $this->assign("edit", 1);
        $this->assign("section", $result);
        $this->display(":work-section-add");
    }

    public function work_section_del()
    {
        $id = intval(I("get.id"));
        $section = M("Worksection");
        $section->where(array("sec_id" => $id))->delete();
        $this->ajaxReturn(sp_ajax_return(array(), "删除成功", 0));
    }

    public function section_batch_del()
    {
        if (IS_POST && IS_AJAX) {
            $ids = $GLOBALS['HTTP_RAW_POST_DATA'];
            $json = (array)json_decode($ids);
            $json = $json['ids'];
            $ids = implode(",", $json);
            LOG::write($ids);
            $section = M("Worksection");
            $section->delete($ids);
            $this->ajaxReturn(sp_ajax_return($json, "success", 0));
        } else {
            $this->ajaxReturn(sp_ajax_return(array(), "invalid request", 1));
        }
    }


    public function dep_unit()
    {
        if (IS_POST && IS_AJAX) {
            $department = M("Department");
            $department->create();
            Log::write(dump($department->data()));
            $department->add();
        } else {
            $this->error("非法请求");
        }
    }

    public function dep_udpate()
    {
        if (IS_POST && IS_AJAX) {
            $department = M("Department");
            $department->create();
            $department->save();
        } else {
            $this->error("非法请求");
        }

    }

    public function dep_list()
    {
        $dep = M("Department");
        $result = $dep->where(array("status" => 1))->field("dep_id,dep_name,dep_note")->order(array("dep_id" => "asc"))->select();

        Log::write(dump($result, false));
        $this->assign("departments", $result);
        $this->display(":locomotive-dep-role");
    }

    public function locomotive_dep_edit()
    {
        $id = intval(I("get.id"));
        $dep = M("Department");
        $result = $dep->where(array("dep_id" => $id))->find();
        Log::write(dump($result, false));
        $this->assign("edit", 1);
        $this->assign("department", $result);
        $this->display(":locomotive-dep-add");
    }

    public function dep_del()
    {
        $id = intval(I("get.id"));
        $dep = M("Department");
        $dep->where(array("dep_id" => $id))->delete();
        $this->ajaxReturn(sp_ajax_return(array(), "删除成功", 0));
    }

    public function department_batch_del()
    {
        if (IS_POST && IS_AJAX) {
            $ids = $GLOBALS['HTTP_RAW_POST_DATA'];
            $json = (array)json_decode($ids);
            $json = $json['ids'];
            $ids = implode(",", $json);
            LOG::write($ids);
            $dep = M("Department");
            $dep->delete($ids);
            $this->ajaxReturn(sp_ajax_return($json, "success", 0));
        } else {
            $this->ajaxReturn(sp_ajax_return(array(), "invalid request", 1));
        }
    }


    public function team_unit()
    {
        if (IS_POST && IS_AJAX) {
            $department = M("Team");
            $department->create();
            Log::write(dump($department->data()));
            $department->add();
        } else {
            $this->error("非法请求");
        }
    }

    public function team_udpate()
    {
        if (IS_POST && IS_AJAX) {
            $department = M("Team");
            $department->create();
            $department->save();
        } else {
            $this->error("非法请求");
        }

    }

    public function team_list()
    {
        $dep = M("Team");
        $result = $dep->where(array("status" => 1))->field("team_id,team_name,team_note")->order(array("team_id" => "asc"))->select();
        Log::write(dump($result, false));
        $this->assign("teams", $result);
        $this->display(":team-role");
    }

    public function team_edit()
    {
        $id = intval(I("get.id"));
        $dep = M("Team");
        $result = $dep->where(array("team_id" => $id))->find();
        Log::write(dump($result, false));
        $this->assign("edit", 1);
        $this->assign("team", $result);
        $this->display(":team-add");
    }

    public function team_del()
    {
        $id = intval(I("get.id"));
        $dep = M("Team");
        $dep->where(array("team_id" => $id))->delete();
        $this->ajaxReturn(sp_ajax_return(array(), "删除成功", 0));
    }

    public function team_batch_del()
    {
        if (IS_POST && IS_AJAX) {
            $ids = $GLOBALS['HTTP_RAW_POST_DATA'];
            $json = (array)json_decode($ids);
            $json = $json['ids'];
            $ids = implode(",", $json);
            LOG::write($ids);
            $dep = M("Team");
            $dep->delete($ids);
            $this->ajaxReturn(sp_ajax_return($json, "success", 0));
        } else {
            $this->ajaxReturn(sp_ajax_return(array(), "invalid request", 1));
        }
    }


    public function issuetype_unit()
    {
        if (IS_POST && IS_AJAX) {
            $department = M("Issuetype");
            $department->create();
            Log::write(dump($department->data()));
            $department->add();
        } else {
            $this->error("非法请求");
        }
    }

    public function issuetype_udpate()
    {
        if (IS_POST && IS_AJAX) {
            $department = M("Issuetype");
            $department->create();
            $department->save();
        } else {
            $this->error("非法请求");
        }

    }

    public function issuetype_list()
    {
        $dep = M("Issuetype");
        $result = $dep->where(array("status" => 1))->field("issuetype_id,issuetype_name,issuetype_note")->order(array("issuetype_id" => "asc"))->select();
        Log::write(dump($result, false));
        $this->assign("issuetypes", $result);
        $this->display(":issuetype-role");
    }

    public function issuetype_edit()
    {
        $id = intval(I("get.id"));
        $dep = M("Issuetype");
        $result = $dep->where(array("issuetype_id" => $id))->find();
        Log::write(dump($result, false));
        $this->assign("edit", 1);
        $this->assign("issuetype", $result);
        $this->display(":issuetype-add");
    }

    public function issuetype_del()
    {
        $id = intval(I("get.id"));
        $dep = M("Issuetype");
        $dep->where(array("issuetype_id" => $id))->delete();
        $this->ajaxReturn(sp_ajax_return(array(), "删除成功", 0));
    }

    public function issuetype_batch_del()
    {
        if (IS_POST && IS_AJAX) {
            $ids = $GLOBALS['HTTP_RAW_POST_DATA'];
            $json = (array)json_decode($ids);
            $json = $json['ids'];
            $ids = implode(",", $json);
            LOG::write($ids);
            $dep = M("Issuetype");
            $dep->delete($ids);
            $this->ajaxReturn(sp_ajax_return($json, "success", 0));
        } else {
            $this->ajaxReturn(sp_ajax_return(array(), "invalid request", 1));
        }
    }


}